package com.kingwang.demo.springsecuritytokendemo.security;

import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.ToString;
import org.springframework.security.core.userdetails.UserDetails;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@RequiredArgsConstructor
@ToString
@EqualsAndHashCode(callSuper = true)
public class InMemoryTokenSession extends AbstractTokenSession {
    @Getter
    private final String id;

    @Getter
    private final String type;

    @Getter
    private final String token;

    @Getter
    @Setter
    private LocalDateTime creationTime;

    @Getter
    @Setter
    private boolean isValid;

    @Getter
    @Setter
    private UserDetails userDetails;

    private Map<String, String> attributes = new ConcurrentHashMap<>();

    private final TokenSessionManager sessionManager;

    @Override
    public Map<String, String> getAttributes() {
        return Collections.unmodifiableMap(this.attributes);
    }

    @Override
    public String getAttribute(String name) {
        return this.attributes.get(name);
    }

    @Override
    public void setAttribute(String name, String value) {
        this.attributes.put(name, value);
    }

    @Override
    public void removeAttribute(String name) {
        this.attributes.remove(name);
    }

    @Override
    public void clearAttributes() {
        this.attributes.clear();
    }
    
    @Override
    public void destroy() {
        this.sessionManager.removeSession(this);
    }
}
